﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CodeGenerator.ClassServices;
using CodeGenerator.Generators;

namespace CodeGenerator.Models
{
    public class LookupClassModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<AttributeModel> _attributes;
        public int ProjectId { get; set; }
        public int ForAttributeId { get; set; }

        internal LookupClassModel(ClassModelDto dto, int attributeId)
        {
            Id = dto.Id;
            Name = dto.Name;
            ProjectId = dto.ProjectId;

            _attributes = new List<AttributeModel>();
            foreach (AttributeModelDto attridto in dto.Attributes)
            {
                AttributeModel attributeModel = new AttributeModel(attridto);
                _attributes.Add(attributeModel);
            }

            this.ForAttributeId = attributeId;
        }

        public LookupClassModel()
        {
            _attributes = new List<AttributeModel>();
        }

        public List<AttributeModel> GetAttributes()
        {
            return _attributes;
        }

        public LookupClassModel GetAllPrimaryKey()
        {
            LookupClassModel newDto = new LookupClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.Pk == true);

            return newDto;
        }

        public LookupClassModel GetAllSearchable()
        {
            LookupClassModel newDto = new LookupClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.Search == true);

            return newDto;
        }

        public LookupClassModel GetAllDisplay()
        {
            LookupClassModel newDto = new LookupClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.Display == true);

            return newDto;
        }

        public LookupClassModel GetAllDisplayInGrid()
        {
            LookupClassModel newDto = new LookupClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.DisplayLengthInGrid > 0);

            return newDto;
        }

        public LookupClassModel GetAllNotNullable()
        {
            LookupClassModel newDto = new LookupClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.Nullable == false);

            return newDto;
        }

        public LookupClassModel GetAllNullable()
        {
            LookupClassModel newDto = new LookupClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.Nullable == true);

            return newDto;
        }
        public LookupClassModel GetAllSearchResult()
        {
            LookupClassModel newDto = new LookupClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.LinkEdit == true);

            return newDto;
        }

        public LookupClassModel GetAllDateTime() //Apple Add Method
        {
            LookupClassModel newDto = new LookupClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.DataType.Name == "DateTime");

            return newDto;
        }
        public LookupClassModel GetAllLookup() //Apple Add Method
        {
            LookupClassModel newDto = new LookupClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.DataType.Name == "Lookup Table");

            return newDto;
        }

        //Weerachai 12-01-2014
        private List<LookupModel> lookupModels = null;

        public List<LookupModel> GetLookupModelByDestClass(ClassModel c)
        {
            if (this.lookupModels == null)
            //this.lookupModels = Generator.GetLookupModelByDestClass(this.Id);
            {
                foreach (AttributeModel a in c._attributes)
                    //if (a.IsLookup && a.LookupClass.Id == this.Id)
                    //    this.lookupModels = a.GetLookupModel();
                    if (a.Id == this.ForAttributeId)
                        this.lookupModels = a.GetLookupModel();
            }
            return this.lookupModels;
        }

        public AttributeModel GetCallerAttributeModel(ClassModel c)
        {
           
                foreach (AttributeModel a in c._attributes)
                    //if (a.IsLookup && a.LookupClass.Id == this.Id)
                    //    this.lookupModels = a.GetLookupModel();
                    if (a.Id == this.ForAttributeId)
                        return a;
            
            return null;
        }

    }
}
